package com.kongchengji.zhanguafou.service;
import com.kongchengji.zhanguafou.constant.WxInfo;
import okhttp3.*;
import org.springframework.stereotype.Service;


@Service
public class CreateMenuService  {

    /**
     * 创建按钮
     */
    public void createMenu(){
        try{
            String json = createMenuDataJson();
            RequestBody requestBody = FormBody.create(MediaType.parse("application/json; charset=utf-8")
                    , json);
            Request request = new Request.Builder()
                    .url(" https://api.weixin.qq.com/cgi-bin/menu/create?access_token="+ WxInfo.accessToken.getToken())//请求的url
                    .post(requestBody)
                    .build();
            OkHttpClient client = new OkHttpClient();
            Response response = client.newCall(request).execute();
            System.out.println(response.body().string());
        } catch (Exception e){
            e.printStackTrace();
        }
    }
    /**
     * 创建按钮传输的数据
     * @return
     */
    public String createMenuDataJson(){
        String json = "{" +
                "     \"button\":[" +
                "     {    " +
                "          \"name\":\"添加监控\"," +
                "           \"sub_button\":[" +
                "             {    " +
                "               \"type\":\"view\"," +
                "               \"name\":\"服务器添加\"," +
                "               \"url\":\"https://open.weixin.qq.com/connect/oauth2/authorize?appid="+WxInfo.APPID+"&redirect_uri="+WxInfo.URL+"/serverConfig&response_type=code&scope=snsapi_userinfo&state=STATE#wechat\""  +
                "             }," +
                "             {    " +
                "               \"type\":\"view\"," +
                "               \"name\":\"站点添加\"," +
                "               \"url\":\"https://open.weixin.qq.com/connect/oauth2/authorize?appid="+WxInfo.APPID+"&redirect_uri="+WxInfo.URL+"/stationConfig&response_type=code&scope=snsapi_userinfo&state=STATE#wechat\""  +
                "             }," +
                "             {    " +
                "               \"type\":\"view\"," +
                "               \"name\":\"备案添加\"," +
                "               \"url\":\"https://open.weixin.qq.com/connect/oauth2/authorize?appid="+WxInfo.APPID+"&redirect_uri="+WxInfo.URL+"/icp/forwardConfig&response_type=code&scope=snsapi_userinfo&state=STATE#wechat\""  +
                "             }]" +
                "       }," +
                "       {" +
                "           \"name\":\"监控查询\"," +
                "           \"sub_button\":[" +
                "             {" +
                "                 \"type\":\"click\"," +
                "                 \"name\":\"服务器\"," +
                "                 \"key\":\"SELECT_SERVER\"" +
                "             },"+
                "             {" +
                "                 \"type\":\"click\"," +
                "                 \"name\":\"站点\"," +
                "                 \"key\":\"SELECT_STATION\"" +
                "             },"+
                "             {" +
                "                 \"type\":\"click\"," +
                "                 \"name\":\"备案\"," +
                "                 \"key\":\"SELECT_ICP\"" +
                "             }]" +
                "       }," +
                "       {"  +
                "           \"name\":\"监控详情\"," +
                "           \"sub_button\":[" +
                "             {    " +
                "               \"type\":\"view\"," +
                "               \"name\":\"查询\"," +
                "               \"url\":\"https://open.weixin.qq.com/connect/oauth2/authorize?appid="+WxInfo.APPID+"&redirect_uri="+WxInfo.URL+"/quartz/openMonitor_detail&response_type=code&scope=snsapi_userinfo&state=STATE#wechat\""  +
                "             }]"+
                "       }]" +
                "}";
        return json;
    }
}
